home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL11.TXT < prev    next >
Text File  |  1986-04-05  |  12KB  |  416 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                           TURBO-LESSONS
  15.  
  16.                      A TURBO Pascal Tutorial
  17.                           Version 1.01
  18.  
  19.  
  20.                     Set # 2 - Lessons 11 - 17
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.                         by Lyle M. Faurot
  28.                           June 27, 1985
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.     Copyright (C), 1985 by Lyle Faurot.  All rights reserved.
  57.  
  58.           TURBO is a trademark of Borland International.
  59. î
  60.                   INTRODUCTION TO TURBO-LESSONS 
  61.  
  62.     When Borland International ran their first ads for TURBO 
  63. PASCAL my reaction was "You gotta be kidding - they can't do 
  64. that".  But, it seems they did!  I've been enjoying this 
  65. programming system ever since.  Apparently a great many other 
  66. programmers are either programming in TURBO or would like to 
  67. learn how.  TURBO-LESSONS were designed to meet that need.  
  68.  
  69.     Each TURBO-LESSON is a bite-size tutorial which introduces 
  70. one or more new concepts.  Each of the lessons are presented in 
  71. the same format which includes the OBJECTIVES of the lesson, and 
  72. the TUTORIAL portion of the lesson.  Most lessons direct you to 
  73. work with a sample program which is provided with these lessons.  
  74.  
  75.     To begin, you should print the TURBO-LESSONS before loading 
  76. TURBO.   
  77.  
  78.  
  79.  
  80.              ****************************************
  81.              D I S T R I B U T I O N      N O T I C E 
  82.              ****************************************
  83.  
  84.  
  85. TURBO-LESSONS are being distributed as USER-SUPPORTED software. 
  86.  
  87. Suggested donation, $5.00, for this second set, lessons 11-17, 
  88. may be sent to: 
  89.  
  90. Lyle Faurot
  91. Box 494
  92. Moorhead, MN   56560
  93.  
  94.  
  95. COPYING BY ORGANIZATIONS
  96.  
  97. Clubs and other non-profit organizations may copy these lessons 
  98. for their members, with the following conditions:
  99.  
  100. 1.  No charge is made for TURBO-LESSONS.  A distribution charge 
  101. may be made to cover the price of the diskette.
  102.  
  103. 2.  Club members are informed that TURBO-LESSONS are distributed 
  104. as user-supported software.
  105.  
  106. 3.  TURBO-LESSONS are distributed unmodified.
  107. î
  108.               T A B L E     O F     C O N T E N T S 
  109.                    (Set # 2,  Lessons 11 - 17)
  110.  
  111.                                                          Page
  112.  
  113. Introduction
  114. Distribution Notice
  115.  
  116. TURBO-LESSON 11: INTRODUCTION TO FUNCTIONS 
  117.      Pascal subprograms  . . . . . . . . . . . . . . . . . 50 *
  118.      FUNCTION declaration  . . . . . . . . . . . . . . . . 50
  119.      User-defined function . . . . . . . . . . . . . . . . 51
  120.  
  121. TURBO-LESSON 12: FUNCTION APPLICATION - ERROR DETECTION
  122.      Error detection . . . . . . . . . . . . . . . . . . . 55
  123.      Using a predefined function . . . . . . . . . . . . . 56
  124.      Writing your own function . . . . . . . . . . . . . . 58
  125.  
  126. TURBO-LESSON 13: STRINGS 
  127.      Strings . . . . . . . . . . . . . . . . . . . . . . . 60
  128.      String replacement statement  . . . . . . . . . . . . 60
  129.      Predefined string function, LENGTH  . . . . . . . . . 63
  130.  
  131. TURBO-LESSON 14: INTRODUCTION TO PROCEDURES
  132.      PROCEDURE declaration . . . . . . . . . . . . . . . . 65
  133.      Using a procedure . . . . . . . . . . . . . . . . . . 66
  134.      Using parameters  . . . . . . . . . . . . . . . . . . 67
  135.      A counter with error checking . . . . . . . . . . . . 68
  136.  
  137. TURBO-LESSON 15: INTERACTIVE SCREEN HANDLING
  138.      Setting up a data entry screen  . . . . . . . . . . . 70
  139.      Being nice to users - ClrScr  . . . . . . . . . . . . 71
  140.      Getting around the screen - GotoXY  . . . . . . . . . 72
  141.      Screen messages and accepting user input  . . . . . . 73
  142.  
  143. TURBO-LESSON 16: REAL NUMBERS
  144.      Range of real numbers . . . . . . . . . . . . . . . . 74
  145.      Input/Output of real numbers  . . . . . . . . . . . . 75
  146.      Calculations with real numbers  . . . . . . . . . . . 76
  147.      Calculations with integers and real numbers . . . . . 78
  148.  
  149. TURBO-LESSON 17: A TIMING FUNCTION
  150.      Include files       . . . . . . . . . . . . . . . . . 79
  151.      A timing function . . . . . . . . . . . . . . . . . . 80
  152.      Improving timing accuracy . . . . . . . . . . . . . . 81
  153.      Sources of errors in timing . . . . . . . . . . . . . 82
  154.  
  155.  
  156.  
  157. * (Note - page 42 was the last page of lesson 10.  Some page 
  158.    numbers were left unused to make it easier to incorporate 
  159.    the changes you suggest.) 
  160. î
  161. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 50
  162.  
  163.  
  164. TURBO-LESSON 11:   INTRODUCTION TO FUNCTIONS
  165.  
  166. OBJECTIVES - In this lesson, you will learn about:
  167.  
  168. 1,  Pascal Subprograms
  169. 2.  FUNCTION declaration
  170. 3.  User-defined function
  171.  
  172.  
  173. 1.  Pascal Subprograms.
  174.  
  175. The example programs in earlier lessons have been rather small.  
  176. As programs grow in size and complexity, it is essential that you 
  177. utilize the appropriate tools to simplify the programming effort.
  178.  
  179. The subprogram is one of the most powerful tools for simplifying 
  180. a complex program.  Subprograms may be viewed as "building 
  181. blocks" for constructing programs.  
  182.  
  183. As you learn to write Pascal programs, you will find Pascal 
  184. subprograms rather easy to write - they are almost identical to 
  185. programs!
  186.  
  187. There are two types of subprograms in Pascal: FUNCTIONS and 
  188. PROCEDURES.   Both types may be either user-defined (written by 
  189. you) or pre-defined as a part of Pascal.  
  190.  
  191. In this lesson you will work with a user-defined FUNCTION.  
  192.  
  193. Most of what you learn about FUNCTIONS also applies to 
  194. PROCEDURES.  
  195.  
  196.  
  197. 2.  FUNCTION declaration.
  198.  
  199. Below is a simplified structure of a Program, Function, and 
  200. Procedure:
  201.  
  202. PROGRAM         FUNCTION       PROCEDURE
  203. LABEL           LABEL          LABEL
  204. CONST           CONST          CONST
  205. TYPE            TYPE           TYPE
  206. VAR             VAR            VAR
  207.  
  208. FUNCTION(s)     FUNCTION(s)    FUNCTION(s)  (0 or more FUNCTIONS)
  209. PROCEDURE(s)    PROCEDURE(s)   PROCEDURE(s) (   and PROCEDURES  )
  210.                          
  211. BEGIN           BEGIN          BEGIN
  212. (processing     (processing    (processing
  213.  statements)     statements)    statements)
  214. END.            END;           END;
  215.  
  216. Notice that they look very similar.    
  217. î
  218. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 51
  219.  
  220.  
  221. One difference, visible here, is the END of the processing block: 
  222. A period follows the END of a PROGRAM block, but a semi-colon 
  223. follows the END of a FUNCTION or PROCEDURE block.  
  224.  
  225.  
  226. 3.  User-defined function.
  227.  
  228. ##### DO:
  229.  
  230. Examine FUNCTION Cube in PROG11 using the editor.
  231.  
  232. FUNCTION Cube(Number:Integer) : Integer;
  233.  
  234. The FUNCTION declaration above means:
  235.  
  236. FUNCTION         Type of subprogram, a FUNCTION, not a PROCEDURE.
  237.  
  238. Cube             The name of the FUNCTION.  
  239.  
  240.                  Cube must be assigned a value by one of the 
  241.                  statements in the FUNCTION BEGIN END block.
  242.                  
  243. (Number:Integer) An integer "parameter" called Number is used 
  244.                  within the FUNCTION.
  245.  
  246.                  This is the number to be cubed.
  247.  
  248. : Integer;       Type of value to be assigned to Cube.
  249.  
  250. ##### DO:
  251.  
  252. Study the connection between the following two statements:
  253.  
  254. In FUNCTION Cube:
  255.  
  256. FUNCTION Cube(Number:Integer) : Integer;
  257.  
  258. In PROG11:
  259.  
  260. WriteLn('The cube is: ', Cube(No) );
  261.  
  262. Cube(No) in the statement above invokes (calls) the FUNCTION Cube 
  263. to operate on the number called No.
  264.  
  265. PROG11,  No ----->  Number, in FUNCTION Cube.
  266.  
  267. The value of the variable called No in PROG11 is provided to the 
  268. FUNCTION to use as a value of its variable called Number.
  269. î
  270. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 52
  271.  
  272.  
  273. ##### DO:
  274.  
  275. Determine how Cube obtains its value in the FUNCTION.
  276.  
  277. Cube := Number * Number * Number;
  278.  
  279. This statement cubes the value in the variable Number (which 
  280. Number received from the variable, No, in the main program) and 
  281. assigns the result to the FUNCTION name, Cube.  
  282.  
  283. The WriteLn statement prints this resulting value in the position 
  284. indicated by the reference to Cube(No) in the WriteLn list.
  285.  
  286. ##### DO:
  287.  
  288. Run PROG11 several times using the following values for input:
  289.  
  290. 3,  -3,  0
  291.  
  292. 31   (cube should be 29791)
  293.  
  294. 32   (cube should be 32768)  Is it?  Remember this problem from 
  295. an earlier lesson?  You will explore some techniques for 
  296. detecting this problem in the next lesson.
  297.  
  298. Now, let's see what happens if you change some of the things in 
  299. the FUNCTION and the reference to it.
  300.  
  301. What would happen if you used a constant instead of No in the 
  302. reference, Cube(No)?
  303.  
  304. ##### DO:
  305.  
  306. Change the WriteLn statement to:
  307.  
  308. WriteLn('The cube is: ', Cube(3) );
  309.  
  310. Run the program several times with different input values.
  311.  
  312. Do you get the same result no matter what you input?
  313.  
  314. Notice that the variable, No, which you input, is no longer used 
  315. in the FUNCTION reference, Cube(3).  
  316.  
  317. What about expressions in the reference to the function?
  318. î
  319. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 53
  320.  
  321.  
  322. ##### DO:
  323.  
  324. Change the FUNCTION reference to Cube(No - 2).
  325.  
  326. Run the program several times with input values: 
  327.  
  328. 5     (No - 2) is 3, so cube should be 27.
  329.  
  330. 1     (No - 2) is -1, so cube should be -1.
  331.  
  332. -1    (No - 2) is -3, so cube should be -27.
  333.  
  334. Do integer expressions work o.k?
  335.  
  336. What if you tried to use a non-integer value in the Cube(No) 
  337. reference?
  338.  
  339. ##### DO:
  340.  
  341. Modify the WriteLn statement to:
  342.  
  343. WriteLn('The cube is: ', Cube('This is a string') )
  344.  
  345. Run the program.  
  346.  
  347. Did the compiler complain?
  348.  
  349. Error 44: Type mismatch.
  350.  
  351. When you press ESC, the cursor stops at the offending reference.
  352.  
  353. The problem: 'This is a string' was given to the function to use 
  354. as an integer value,  but it is a string, not an integer.
  355.  
  356.  
  357. A good way to learn to recognize errors, is to introduce one 
  358. error at a time and check the results.  
  359.  
  360. Let's look at some errors which you might make in setting up the 
  361. FUNCTION itself.  
  362. î
  363. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 54
  364.  
  365.  
  366. ##### DO:
  367.  
  368. Change the type of Number to Char.  The FUNCTION declaration 
  369. should appear:
  370.  
  371. FUNCTION Cube(Number:Char) : Integer;
  372.  
  373. What happens when you attempt to run or compile the program?
  374.  
  375. Error 47: Operand type(s) does not match operator.
  376.  
  377. When you press ESC, the cursor stops, not at the line you 
  378. modified, but at the line:
  379.  
  380. Cube := Number * Number * Number;
  381.  
  382. Why?
  383.  
  384. There is nothing wrong with the line you changed.  It may not do 
  385. what you want it to, but to the computer, it is just an Integer 
  386. FUNCTION with a character for input.  
  387.  
  388. The problem appears (to the compiler) when an attempt is made to 
  389. multiply a character times a character in the line marked by the 
  390. cursor.  At this point the compiler alerts you to the problem.
  391.  
  392. DEBUGGING NOTE: DON'T FORGET, THE CURSOR POINTS TO THE PLACE 
  393.                 WHERE THE ERROR WAS DETECTED BY THE COMPILER.  
  394.                 THE CAUSE OF THE ERROR MAY BE ELSEWHERE!  
  395.  
  396. What would happen if you inadvertently designated the function as 
  397. something other than Integer?
  398.  
  399. ##### DO:
  400.  
  401. Change the FUNCTION declaration to:
  402.  
  403. FUNCTION Cube(Number:Integer) : Char;
  404.  
  405. Run the program.    What results did you get?
  406.  
  407. Error 44 again - Type mismatch.  
  408.  
  409. Again, the cursor points to the calculation.  
  410.  
  411. This time though, the calculation itself is o.k.    The problem 
  412. occurs when an attempt is made to assign the integer result to 
  413. the Character FUNCTION, Cube.
  414. î
  415.  
  416.